// gjw/pages/dingzhi/store.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
		user_identity:-1,
    tab: ['定制商家', '为您定制', '我要定制'],
    tabIndex: 0,
		activeNav: 0,
		activeIndex: 0,
    menu: [],
    index: 0,
    list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    app.route(this)
    app.location_attachurl(this)
    app.location_url(this)
		app.util.request({
			url: 'entry/wxapp/Isregister',
			data: { openid: wx.getStorageSync('openid') },
			success: function (res) {
				if (res.data.code == 200) {
					// 用户身份
					that.setData({
						user_identity:1
					})
					app.util.request({
						url: 'entry/wxapp/Iscustomers',
						data: { openid: wx.getStorageSync('openid') },
						success: function (cres) {
							if (res.data.code == 200) {
								// 标识用户身份 定制商家
								that.setData({
									user_identity: 0
								})
							} else if (res.data.code == 204) {
								//普通商家
								that.setData({
									user_identity: 2
								})
							} 
						}
					})
				} else {
					// 游客
					that.setData({
						user_identity: -1
					})
				}
			}
		})
    // 查询分类
    app.util.request({
      url: 'entry/wxapp/catestop',
      success: res => {
        var nav = res.data.res
        that.setData({
          nav: nav,
          store_nav_id: nav[0].id
        })
        that.nav_sele(0, 0, nav[0].id)
      }
		})
		that.store_identy()
  },

  // tabbar.js
  tabbar: function(e) {

    var that = this
    var url = e.currentTarget.dataset.url
    wx.reLaunch({
      url: url,
    })
  },

  store_identy: function(e) {
    var that = this
    if (that.data.activeNav == 0) {
      that.more_stores()
    } else if (that.data.activeNav == 1) {
      that.sele_store()
    } else if (that.data.activeNav == 2) {
      that.user_store()
    }
  },
  // 切换tab选项
  tab: function(e) {
    var index = e.currentTarget.dataset.tabindex
    if (index == 0) {
      this.setData({
        activeNav: index,
        activeIndex: 0
      })
      this.more_stores(this.data.store_nav_id)
    } else if (index == 1) {
      this.setData({
        activeNav: index,
        activeIndex: -1
      })
      this.sele_store()
    } else if (index == 2) {
      this.setData({
        activeNav: index,
        activeIndex: -1
      })
      this.user_store()
    }
  },
  // 选择分类
  tabClick: function(e) {
    var that = this,
    nav = that.data.nav,
		activeNav = that.data.activeNav
    if (e != null) {
      var activeIndex = e.currentTarget.dataset.index,
        nav_id = e.currentTarget.dataset.id
      that.setData({
        activeIndex: activeIndex,
        nav_id: nav_id
      })
      that.nav_sele(activeNav, activeIndex, nav_id)
    } else {
      var activeIndex = that.data.activeIndex,
        nav_id = that.data.nav_id
      that.setData({
        activeIndex: activeIndex,
        nav_id: nav_id
      })
      that.nav_sele(activeNav, activeIndex, nav_id)
    }
  },
  nav_sele: function(activeNav, activeIndex, nav_id) {
    var that = this
    if (activeNav == 0) {
      that.more_stores(nav_id)
    } else if (activeNav == 1) {
      if (activeIndex == -1) {
        that.sele_store()
      } else {
        that.sele_stores(nav_id)
      }
    } else if (activeNav == 2) {
      if (activeIndex == -1) {
        that.user_store()
      } else {
        that.user_stores(nav_id)
      }
    }
  },
  // 选择商家信息----推荐页
  more_store: function(e) {
    var that = this
    app.util.request({
      url: 'entry/wxapp/Pricusup',
      success: res => {

        if (res.data.code == 200) {
          var post_list = res.data.res
          for (let i in post_list) {
            if (post_list[i].bannerimg != '') {
              post_list[i].bannerimg = post_list[i].bannerimg.split(",")
            }
          }
          that.setData({
            post_list: post_list
          })
        } else {
          that.setData({
            post_list: []
          })

        }
      }
    })
  },
  // 选择商家信息----不推荐
  more_stores: function(Pricuscateid) {
    var that = this
    app.util.request({
      url: 'entry/wxapp/Pricuscateid',
      data: {
        shot_id: Pricuscateid
      },
      success: res => {

        if (res.data.code == 200) {
          var post_list = res.data.res
          for (let i in post_list) {
            if (post_list[i].bannerimg != '') {
              post_list[i].bannerimg = post_list[i].bannerimg.split(",")
            }
          }
          that.setData({
            post_list: post_list
          })
        } else {
          that.setData({
            post_list: []
          })

        }
      }
    })
  },
  // 选择商家发的帖子----推荐页
  sele_store: function(e) {
    var that = this
    app.util.request({
			url: 'entry/wxapp/pricup',
      success: res => {
        if (res.data.code == 200) {
          var post_list = res.data.res
          for (let i in post_list) {
            if (post_list[i].img != '') {
              post_list[i].img = post_list[i].img.split(",")
            }
          }
          //post_list.reverse()
          that.setData({
            post_list: post_list
          })
        } else {
          that.setData({
            post_list: []
          })
        }
      }
    })
  },
  // 选择商家发的帖子----不推荐
  sele_stores: function(nav_id) {
    var that = this
    app.util.request({
      url: 'entry/wxapp/Priccateid',
      data: {
        c_id: nav_id
      },
      success: res => {
        if (res.data.code == 200) {
          var post_list = res.data.res
          for (let i in post_list) {
            if (post_list[i].img != '') {
              post_list[i].img = post_list[i].img.split(",")
            }
          }
          that.setData({
            post_list: post_list
          })
        } else {
          that.setData({
            post_list: []
          })
        }
      }
    })
  },
  // 选择用户发的帖子----推荐页
  user_store: function(e) {
    var that = this
    app.util.request({
      url: 'entry/wxapp/Priuup',
      success: res => {

        if (res.data.code == 200) {
          var post_list = res.data.res
          for (let i in post_list) {
            if (post_list[i].img != '') {
              post_list[i].img = post_list[i].img.split(",")
            }
          }
          //post_list.reverse()
          that.setData({
            post_list: post_list
          })
        } else {
          that.setData({
            post_list: []
          })
        }
      }
    })
  },
  // 选择用户发的帖子----不推荐
  user_stores: function(nav_id) {
    var that = this
    app.util.request({
      url: 'entry/wxapp/Priucateid',
      data: {
        c_id: nav_id
      },
      success: res => {

        if (res.data.code == 200) {
          var post_list = res.data.res
          for (let i in post_list) {
            if (post_list[i].img != '') {
              post_list[i].img = post_list[i].img.split(",")
            }
          }
          that.setData({
            post_list: post_list
          })
        } else {
          that.setData({
            post_list: []
          })
        }
      }
    })
  },
  Customized: function(e) {
    var type = e.currentTarget.dataset.type,
      user_identity = this.data.user_identity
    if (this.data.activeNav == 2) {
      
			if (user_identity == '-1') {
				wx.showModal({
					title: '温馨提示',
					content: '请先注册！',
					success: res => {
						if (res.confirm) {
							wx.navigateTo({
								url: '../index/register',
							})
						}
					}
				})
			}else{
				wx.navigateTo({
					url: 'post?type=' + 2,//我要定制
				})
			}
    } else if (this.data.activeNav == 1) {
      if (user_identity == 0) {
        wx.navigateTo({
          url: 'post?type=' + 1,//为您定制
        })

      } else {
        wx.showModal({
          title: '温馨提示',
          content: '您还不是定制商家，点击去商家认证',
          success: res => {
            if (res.confirm) {
							wx.navigateTo({
								url: '../store/register',
							})
            }
          }
        })
      }
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this
    that.setData({
      activeIndex: that.data.activeIndex,
      activeNav: that.data.activeNav,
      post_list: []
    })

    // that.store_identy()
    that.tabClick()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    var activeNav = that.data.activeNav
    var activeIndex = that.data.activeIndex
    var nav_id = that.data.nav_id
    that.data.page = that.data.page + 1
    if (activeNav == 0) {

      if (activeIndex == -1) { //推荐
        app.util.request({
          url: 'entry/wxapp/Pricusup',
          data: {
            page: that.data.page
          },
          success: res => {

            if (res.data.code == 500) {
              wx.showToast({
                title: '没有新的数据了！',
                icon: 'none'
              })
            } else if (res.data.code == 200) {
              for (let j in res.data.res) {
                res.data.res[j].bannerimg = res.data.res[j].bannerimg.split(',')
              }
              for (var i = 0; i < res.data.res.length; i++) {
                that.data.post_list.push(res.data.res[i])
              }
              that.setData({
                post_list: that.data.post_list
              })

            }
          }
        })
      } else {
        app.util.request({
          url: 'entry/wxapp/Pricuscateid',
          data: {
            shot_id: nav_id,
            page: that.data.page
          },
          success: res => {

            if (res.data.code == 500) {
              wx.showToast({
                title: '没有新的数据了！',
                icon: 'none'
              })
            } else if (res.data.code == 200) {
              for (let j in res.data.res) {
                res.data.res[j].bannerimg = res.data.res[j].bannerimg.split(',')
              }
              for (var i = 0; i < res.data.res.length; i++) {
                that.data.post_list.push(res.data.res[i])
              }
              that.setData({
                post_list: that.data.post_list
              })

            }
          }
        })
      }
    } else if (activeNav == 1) {
      if (activeIndex == -1) { //推荐
        app.util.request({
          url: 'entry/wxapp/Pricup',
          data: {
            page: that.data.page
          },
          success: res => {

            if (res.data.code == 500) {
              wx.showToast({
                title: '没有新的数据了！',
                icon: 'none'
              })
            } else if (res.data.code == 200) {
              for (let j in res.data.res) {
                res.data.res[j].img = res.data.res[j].img.split(',')
              }
              for (var i = 0; i < res.data.res.length; i++) {
                that.data.post_list.push(res.data.res[i])
              }
              that.setData({
                post_list: that.data.post_list
              })

            }
          }
        })
      } else {
        app.util.request({
          url: 'entry/wxapp/Priccateid',
          data: {
            c_id: nav_id,
            page: that.data.page
          },
          success: res => {

            if (res.data.code == 500) {
              wx.showToast({
                title: '没有新的数据了！',
                icon: 'none'
              })
            } else if (res.data.code == 200) {
              for (let j in res.data.res) {
                res.data.res[j].img = res.data.res[j].img.split(',')
              }
              for (var i = 0; i < res.data.res.length; i++) {
                that.data.post_list.push(res.data.res[i])
              }
              that.setData({
                post_list: that.data.post_list
              })

            }
          }
        })
      }

    } else if (activeNav == 2) {
      if (activeIndex == -1) { //推荐
        app.util.request({
          url: 'entry/wxapp/Priuup',
          data: {
            page: that.data.page
          },
          success: res => {

            if (res.data.code == 500) {
              wx.showToast({
                title: '没有新的数据了！',
                icon: 'none'
              })
            } else if (res.data.code == 200) {
              for (let j in res.data.res) {
                res.data.res[j].img = res.data.res[j].img.split(',')
              }
              for (var i = 0; i < res.data.res.length; i++) {
                that.data.post_list.push(res.data.res[i])
              }
              that.setData({
                post_list: that.data.post_list
              })

            }
          }
        })
      } else {
        app.util.request({
          url: 'entry/wxapp/Priucateid',
          data: {
            c_id: nav_id,
            page: that.data.page
          },
          success: res => {

            if (res.data.code == 500) {
              wx.showToast({
                title: '没有新的数据了！',
                icon: 'none'
              })
            } else if (res.data.code == 200) {
              for (let j in res.data.res) {
                res.data.res[j].img = res.data.res[j].img.split(',')
              }
              for (var i = 0; i < res.data.res.length; i++) {
                that.data.post_list.push(res.data.res[i])
              }
              that.setData({
                post_list: that.data.post_list
              })

            }
          }
        })
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})